require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsRefineHandler={};
local this = RedDotsRefineHandler;

local crystalConfig = nil;

function RedDotsRefineHandler.init()
    CloseEvent(Config.EventType.Fresh_refine,this.update);
    CloseEvent(Config.EventType.Fresh_Equip,this.update);
    CloseEvent(Config.EventType.Fresh_Currency,this.update);
    ListenEvent(Config.EventType.Fresh_Currency,this.update);
    ListenEvent(Config.EventType.Fresh_Equip,this.update);
    ListenEvent(Config.EventType.Fresh_refine,this.update);
    return this;
end


--参数 
function RedDotsRefineHandler.getFlag(...)
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.EquipRefine, false) then
        local  itemList = {}
        logicMgr.ItemManager.getItemDatabyItemType( EnumConst.ItemTypeEnum.Refine,itemList);
        logicMgr.ItemManager.getItemDatabyItemType( EnumConst.ItemTypeEnum.EquipStepstone,itemList);
        logicMgr.EquipManager.getEquipList(itemList)
        table.sort(itemList, this.sort)

        local partnerId, data =...;
        local dataList = {};
        if partnerId == nil then
            table.insert(dataList, data);
        else
            local equips = logicMgr.EquipManager.getHeroEquipList(partnerId);
            for _, v1 in pairs(equips) do
                if v1.part == EnumConst.EquipPartEnum.HAND or v1.part == EnumConst.EquipPartEnum.HEAD or v1.part == EnumConst.EquipPartEnum.BODY or v1.part == EnumConst.EquipPartEnum.LEG then
                    table.insert(dataList, v1);
                end
            end
        end
        for _, v in pairs(dataList) do
            local sum=0;
            local num=0;
            local itemExp=0;
            local tableData;
            local flag = v;
            local currefine=dataTable.getDataTableByMultiId("t_equiprefine",flag.itemModelId,flag["curRfLevel"])
            local nextfine=dataTable.getDataTableByMultiId("t_equiprefine",flag.itemModelId,flag["curRfLevel"] + 1)
            if itemList~=nil and nextfine ~= nil and currefine~=nil then
                for key, value in pairs(itemList) do
                    if flag.quality>value.quality then
                        if value.itemType==EnumConst.ItemTypeEnum.Refine then
                            itemExp = decodeJsonStrToTabel(value["itemFunction"])[2];
                        elseif value.itemType==EnumConst.ItemTypeEnum.EquipStepstone then
                            itemExp = decodeJsonStrToTabel(value["itemFunction"])[2];
                        else
                            tableData = dataTable.getDataTableByMultiId("t_equiprefine",value.itemModelId,value["curRfLevel"])
                            itemExp = tableData.f_EquipBaseExp
                        end
                        num = math.min(math.ceil((currefine.f_EquipRefineLevelExp - sum) / itemExp), value.num);
                        sum = sum + num * itemExp;
                        if sum >= currefine.f_EquipRefineLevelExp then
                            num = sum * currefine.f_EquipRefineGoldCo;
                            if num <= logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.MONEY) then
                                return Config.RedDotsFlagState.Needful;
                            end
                            break;
                        end
                    end
                end
            end
        end
    end
    return Config.RedDotsFlagState.None;
end

function RedDotsRefineHandler.sort(a, b)
    --装备【品质（从低到高）→部位（1到4）
    if a.itemType ~= b.itemType then
        if a.itemType == EnumConst.ItemTypeEnum.Refine or b.itemType == EnumConst.ItemTypeEnum.Refine then return a.itemType == EnumConst.ItemTypeEnum.Refine; end
        if a.itemType == EnumConst.ItemTypeEnum.Equip or b.itemType == EnumConst.ItemTypeEnum.Equip then return a.itemType == EnumConst.ItemTypeEnum.Equip; end
        return a.itemType == EnumConst.ItemTypeEnum.EquipStepstone;
    end
    if a.itemType == EnumConst.ItemTypeEnum.Equip then
        if a.quality ~= b.quality then return a.quality < b.quality; end
        return a.part < b.part;
    elseif a.itemType == EnumConst.ItemTypeEnum.Refine then
        return a.itemModelId < b.itemModelId;
    elseif a.itemType == EnumConst.ItemTypeEnum.EquipStepstone then
        return a.itemModelId < b.itemModelId;
    end
    return true;
end

function RedDotsRefineHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.refine);
end